#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © 2018 crane <crane@crane-pc>
#
# Distributed under terms of the MIT license.

"""

"""

from random import random


class Cond(object):
    ABOVE = 1
    DOWN  = 0

    DETACTED = 1
    NODETACTED = 0

    def __init__(self):
        self.experiment_results = []

    def one_experiment(self):
        # gene plane position
        plane = random()
        if plane < self.scenario_prop[0][0]:
            plane = self.ABOVE
            index = 0
        else:
            plane = self.DOWN
            index = 1

        detacted = random()
        if detacted < self.scenario_prop[index][1][0]:
            detacted = self.DETACTED
        else:
            detacted = self.NODETACTED

        self.experiment_results.append((plane, detacted))

    def calc_result(self):
        # print(len(self.experiment_results))
        detacted_cnt = 0
        above_cnt = 0

        for above, detacted in self.experiment_results:
            if detacted:
                detacted_cnt += 1
            if above and detacted:
                above_cnt += 1

        # print()
        print("corrected detacted: [%s]" % (above_cnt / detacted_cnt))
        above = self.scenario_prop[0][0]
        down  = self.scenario_prop[1][0]
        above_detacted = self.scenario_prop[0][1][0]
        down_detacted  = self.scenario_prop[1][1][0]

        print("byes: %s" % ((above * above_detacted) / \
                            (above * above_detacted + down * down_detacted)))

    def monte_condition(self, n=10 ** 6, scenario_prop=None):
        self.n = n
        self.scenario_prop = scenario_prop

        for i in range(self.n):
            self.one_experiment()

        self.calc_result()


def main():
    print("start main")
    # monte_condition(10 ** 5)
    cond = Cond()
    cond.monte_condition(10 ** 6, [
        [0.055, [0.99, 0.01]],
        [0.945, [0.10, 0.90]], ]
        # [0.055, [0.99, 0.01]],
        # [0.945, [0.01, 0.99]], ]
    )

if __name__ == "__main__":
    main()
